package com.supersit.greensproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.adapter.SubjectDataAdapter;
import com.supersit.greensproject.application.MyApplication;
import com.supersit.greensproject.fragment.MassDetectionEnteringFragmentDairyCattle;
import com.supersit.greensproject.fragment.MassDetectionEnteringFragmentFish;
import com.supersit.greensproject.fragment.MassDetectionEnteringFragmentPig;
import com.supersit.greensproject.fragment.MassDetectionEnteringFragmentTablePoultry;
import com.supersit.greensproject.fragment.MassDetectionEnteringFragmentVegetables;
import com.supersit.greensproject.fragment.MassDetectionSelectFragment;
import com.supersit.greensproject.resources.Authority;
import com.supersit.greensproject.resources.Resources;
import com.supersit.greensproject.view.MyTitleBar;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by cong on 2016/7/16.
 */
@ContentView(R.layout.activity_mass_detection)
public class MassDetectionActivity extends BaseActivity {
    @ViewInject(R.id.lv_mass_detection_menu)
    private ListView lvMassDetectionMenu;
    private ArrayList<Map<Integer, String>> itemData;
    private SubjectDataAdapter adapter;
    private Context context;
    private MyTitleBar titleBar;
    private int productType;//0蔬菜 1肉鸡 2奶牛 3渔业 4生猪

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        setTitleBar();
        bindListener();
        setListViewData();
    }

    private void setTitleBar() {
        titleBar = new MyTitleBar(context, findViewById(R.id.mass_detectioon_title));
        titleBar.setTitleText(getResources().getString(R.string.mass_detectioon));
        titleBar.setChildVisibility(false, titleBar.getmImgRight());

    }

    private void bindListener() {
        titleBar.setImg1Listener(onClickListener);
        lvMassDetectionMenu.setOnItemClickListener(onItemClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_left:
                    finish();
                    break;
            }
        }
    };

    private void setListViewData() {
        int[] qualityMonitorAuthority = Authority.getQualityMonitorAuthority(context);
        itemData = new ArrayList<Map<Integer, String>>();
        for (int i = 0; i < Resources.uploadDataMenu.length; i++) {
            for (int j = 0; j < qualityMonitorAuthority.length; j++) {
                if (i == qualityMonitorAuthority[j]) {
                    Map<Integer, String> map = new HashMap<Integer, String>();
                    map.put(i, Resources.uploadDataMenu[i]);
                    itemData.add(map);
                }
            }
        }
        adapter = new SubjectDataAdapter(context
                , itemData, Resources.NoMessige);
        lvMassDetectionMenu.setAdapter(adapter);
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(context, MassDetectionFragmentActivity.class);
            MassDetectionSelectFragment massDetectionSelectFragment = new MassDetectionSelectFragment();
            int num = 0;
            Map<Integer, String> item = itemData.get(position);
            for (int key : item.keySet()) {
                num = key;
            }
            switch (num) {
                case 0:
                    MyApplication.list.clear();
                    MassDetectionEnteringFragmentVegetables massDetectionEnteringFragmentVegetables = new MassDetectionEnteringFragmentVegetables();
                    MyApplication.list.add(massDetectionSelectFragment);
                    MyApplication.list.add(massDetectionEnteringFragmentVegetables);
                    productType = num;
                    intent.putExtra("productType", 0);
                    startActivity(intent);
                    break;
                case 1:
                    MyApplication.list.clear();
                    MassDetectionEnteringFragmentTablePoultry massDetectionEnteringFragmentTablePoultry = new MassDetectionEnteringFragmentTablePoultry();
                    MyApplication.list.add(massDetectionSelectFragment);
                    MyApplication.list.add(massDetectionEnteringFragmentTablePoultry);
                    productType = num;
                    intent.putExtra("productType", 1);
                    startActivity(intent);
                    break;
                case 2:
                    MyApplication.list.clear();
                    MassDetectionEnteringFragmentDairyCattle massDetectionEnteringFragmentDairyCattle = new MassDetectionEnteringFragmentDairyCattle();
                    MyApplication.list.add(massDetectionSelectFragment);
                    MyApplication.list.add(massDetectionEnteringFragmentDairyCattle);
                    productType = num;
                    intent.putExtra("productType", 2);
                    startActivity(intent);
                    break;
                case 3:
                    MyApplication.list.clear();
                    MassDetectionEnteringFragmentFish massDetectionEnteringFragmentFish = new MassDetectionEnteringFragmentFish();
                    MyApplication.list.add(massDetectionSelectFragment);
                    MyApplication.list.add(massDetectionEnteringFragmentFish);
                    productType = num;
                    intent.putExtra("productType", 3);
                    startActivity(intent);
                    break;
                case 4:
                    MyApplication.list.clear();
                    MassDetectionEnteringFragmentPig massDetectionEnteringFragmentPig = new MassDetectionEnteringFragmentPig();
                    MyApplication.list.add(massDetectionSelectFragment);
                    MyApplication.list.add(massDetectionEnteringFragmentPig);
                    productType = num;
                    intent.putExtra("productType", 4);
                    startActivity(intent);
                    break;
            }
        }
    };
}
